import { Body, Controller, Post, UseGuards, UsePipes, ValidationPipe } from '@nestjs/common';
import { UserService } from './user.service';
import { AuthService } from '../auth/auth.service';
import { AuthGuard } from '@nestjs/passport';
import { LoginDTO, RegisterInfoDTO } from './user.dto';
import { ResultData } from 'src/utils/result';
import { ApiBearerAuth, ApiBody, ApiTags } from '@nestjs/swagger';

@ApiBearerAuth() // Swagger 的 JWT 验证
@ApiTags('user')
@Controller('user')
export class UserController {
  
    constructor(private readonly authService: AuthService, private readonly usersService: UserService) {}


    @Post('find-one')
    @UseGuards(AuthGuard('jwt')) // 使用 'JWT' 进行验证
    findOne(@Body() body: any) {
      return this.usersService.findOne(body.username);
    }
    
    
    @UsePipes(new ValidationPipe()) // 使用管道验证
    @Post('register')
    @ApiBody({
      description: '用户注册',
      type: RegisterInfoDTO
    })
    async register(@Body() body: RegisterInfoDTO) {// 指定 DTO类型
     return await this.usersService.register(body);
    }

     // JWT验证 - Step 1: 用户请求登录
  @UsePipes(new ValidationPipe()) // 使用管道验证
  @Post('login')
  @ApiBody({
    description: '用户登录',
    type: LoginDTO
  })
  async login(@Body() loginParmas: LoginDTO): Promise<ResultData>  {
    console.log('JWT验证 - Step 1: 用户请求登录');
    return  await this.authService.validateUser(loginParmas.username, loginParmas.password);
   
  }



}
